package io.gitee.hechenl.datamultitrans.task;

import cn.hutool.core.date.DateUtil;
import io.gitee.hechenl.datamultitrans.constants.DateFormat;
import io.gitee.hechenl.datamultitrans.constants.MqttTopic;
import io.gitee.hechenl.datamultitrans.constants.Cron;
import io.gitee.hechenl.datamultitrans.domain.dto.TransTemperatureDTO;
import io.gitee.hechenl.datamultitrans.mqtt.QosEnum;
import io.gitee.hechenl.datamultitrans.service.IDataOriginService;
import io.gitee.hechenl.datamultitrans.service.IDataTransService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 温度相关的推送
 */
@Slf4j
@Component
public class TemperatureTask {

    @Autowired
    private IDataOriginService dataOriginService;
    @Autowired
    private IDataTransService dataTransService;

    @Scheduled(cron = Cron.PER_MINUTES_1)
    public void run() {
        String time = String.format("TemperatureTask running, %s", DateUtil.format(LocalDateTime.now(), DateFormat.TIME_ALL));
        log.debug("TemperatureTask running {}", time);
        TransTemperatureDTO temperature = dataOriginService.getTemperature();
        dataTransService.mqttPublish(temperature, MqttTopic.TEMPERATURE, QosEnum.QOS_0);
    }
}
